package com.ca.ridersclubapplication.request;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.ca.ridersclubapplication.data.DataHandler;
import com.ca.ridersclubapplication.data.entity.Handleable;
import com.ca.ridersclubapplication.data.entity.ListEntity;
import com.ca.ridersclubapplication.request.dto.Result;

import java.util.List;

import lombok.AllArgsConstructor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class APICallback<T> implements Callback<Result<T>> {

    private final Activity activity;
    private final TextView tipView;
    private final AfterExecutor<T> executor;

    private Result<T> result;
    private boolean success;
    private T data;
    private String message;

    public APICallback(Activity activity, TextView tipView, AfterExecutor<T> executor) {
        this.activity = activity;
        this.tipView = tipView;
        this.executor = executor;
    }

    public APICallback(Activity activity, TextView tipView) {
        this.activity = activity;
        this.tipView = tipView;
        this.executor = null;
    }

    public APICallback(Activity activity, AfterExecutor<T> executor) {
        this.activity = activity;
        this.executor = executor;
        tipView = null;
    }

    public APICallback(Activity activity) {
        this.activity = activity;
        tipView = null;
        this.executor = null;
    }

    @Override
    public void onResponse(@NonNull Call<Result<T>> call, @NonNull Response<Result<T>> response) {

        result = response.body();
        if (result == null) {
            Log.e("APICallback Failed", "body is null.");
            return;
        }
        if (!result.isSuccess()) {
            if (tipView != null) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(() -> tipView.setText(result.getMessage()));
                tipView.setVisibility(View.VISIBLE);
            }

            //  认证失败时，重新登录
            if ("认证失败".equals(result.getMessage())) {
                DataHandler.logout(activity);
                return;
            }

            if (result.getMessage() != null) {
                Log.e("Call Error", result.getMessage());
            }
        }
        else if (tipView != null ) {
            tipView.setText("");
        }

        //  请求成功
        success = result.isSuccess();
        data = result.getData();
        message = result.getMessage();

        //  将数据放入数据处理器中
        if (data instanceof Handleable) {
            Handleable handleable = (Handleable)  data;
            DataHandler.putData(handleable);
        }

        //  执行后续任务
        if (executor != null) {
            executor.execute(result);
        }
    }

    @Override
    public void onFailure(@NonNull Call<Result<T>> call, @NonNull Throwable t) {
        t.printStackTrace();
    }

    public Activity getActivity() {
        return activity;
    }

    public T getData() {
        return data;
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMessage() {
        return message;
    };

    public Result<T> getResult() {
        return result;
    }
}
